package com.ruoyi.web.controller.sports;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sports.domain.Score;
import com.ruoyi.sports.service.IScoreService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 参赛成绩Controller
 * 
 * @author ZJH
 * @date 2023-03-08
 */
@RestController
@RequestMapping("/sports/score")
public class ScoreController extends BaseController
{
    @Autowired
    private IScoreService scoreService;

    /**
     * 查询参赛成绩列表
     */
    @PreAuthorize("@ss.hasPermi('sports:score:list')")
    @GetMapping("/list")
    public TableDataInfo list(Score score)
    {
        startPage();
        List<Score> list = scoreService.selectScoreList(score);
        return getDataTable(list);
    }

    /**
     * 查询参赛成绩列表
     */
    @PreAuthorize("@ss.hasPermi('sports:score:list')")
    @GetMapping("/myList")
    public TableDataInfo myList(Score score)
    {
        startPage();
        List<Score> list = scoreService.selectMyScoreList(score);
        return getDataTable(list);
    }

    /**
     * 导出参赛成绩列表
     */
    @PreAuthorize("@ss.hasPermi('sports:score:export')")
    @Log(title = "参赛成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Score score)
    {
        List<Score> list = scoreService.selectScoreList(score);
        ExcelUtil<Score> util = new ExcelUtil<Score>(Score.class);
        util.exportExcel(response, list, "参赛成绩数据");
    }

    /**
     * 获取参赛成绩详细信息
     */
    @PreAuthorize("@ss.hasPermi('sports:score:query')")
    @GetMapping(value = "/{scoreId}")
    public AjaxResult getInfo(@PathVariable("scoreId") Long scoreId)
    {
        return success(scoreService.selectScoreByScoreId(scoreId));
    }

    /**
     * 新增参赛成绩
     */
    @PreAuthorize("@ss.hasPermi('sports:score:add')")
    @Log(title = "参赛成绩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Score score)
    {
        return toAjax(scoreService.insertScore(score));
    }

    /**
     * 修改参赛成绩
     */
    @PreAuthorize("@ss.hasPermi('sports:score:edit')")
    @Log(title = "参赛成绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Score score)
    {
        return toAjax(scoreService.updateScore(score));
    }

    /**
     * 删除参赛成绩
     */
    @PreAuthorize("@ss.hasPermi('sports:score:remove')")
    @Log(title = "参赛成绩", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scoreIds}")
    public AjaxResult remove(@PathVariable Long[] scoreIds)
    {
        return toAjax(scoreService.deleteScoreByScoreIds(scoreIds));
    }

    @Log(title = "参赛成绩录入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sports:score:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Score> util = new ExcelUtil<Score>(Score.class);
        List<Score> scoreList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = scoreService.importScore(scoreList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Score> util = new ExcelUtil<Score>(Score.class);
        util.importTemplateExcel(response, "参赛成绩数据");
    }
}
